package hust.music.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import hust.music.dao.UserDao;
import hust.music.dao.base.JpaDaoBase;
import hust.music.domain.User;

@Repository("userDao")
public class UserDaoImpl extends JpaDaoBase implements UserDao {

	@PersistenceContext
	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean isUserExist(String username) {
		Query query = entityManager
				.createQuery("from User where username = ?1");
		List result = query.setParameter(1, username).getResultList();
		if (result.size() == 1) {
			return true;
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public User login(User user) {
		Query query = entityManager
				.createQuery("from User where username = ?1");
		List result = query.setParameter(1, user.getUsername()).getResultList();
		if (result.size() == 1) {
			user = (User) query.getSingleResult();
			return user;
		}
		return null;
	}
}
